﻿package com.tudou.newPlayer
{
    import com.tudou.log.*;
    import com.tudou.model.*;
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.media.*;
    import flash.net.*;
    import flash.utils.*;

    public class BaseVideoPlayer extends Sprite
    {
        protected var bugBufferFull:Boolean = false;
        protected var netStream:NetStream;
        protected var _mute:Boolean = false;
        protected var _videoStatus:VideoStatus;
        protected var firstBufferEmpty:Boolean = false;
        protected var background:Shape;
        protected var netConnetion:NetConnection;
        protected var _metaData:MetaData;
        protected var _volume:Number = 1;
        protected var _bufferTime:int = 2;
        protected var _playURL:String;
        protected var playerTimer:Timer;
        protected var video:Video;
        static const PLAYER_CLOCK:Number = 50;
        private static const INFO_STATUS:String = "status";
        private static const INFO_ERROR:String = "error";

        public function BaseVideoPlayer(param1:uint = 400, param2:uint = 300)
        {
            var _loc_3:* = RectDisplayUtil.getShape(param1, param2, 0, 0.6);
            this.mask = _loc_3;
            addChild(_loc_3);
            background = RectDisplayUtil.getShape(param1, param2, 0, 0.6);
            addChild(background);
            Logger.getInst().debug(this, param1, param2);
            video = new Video(param1, param2);
            video.smoothing = true;
            video.deblocking = 1;
            addChild(video);
            resetBody();
            _videoStatus = new VideoStatus();
            changeStatus(VideoStatus.INITED);
            playerTimer = new Timer(PLAYER_CLOCK);
            playerTimer.addEventListener(TimerEvent.TIMER, playerTimerHlr);
            setWidthHeight(param1, param2);
            playerTimer.start();
            return;
        }// end function

        protected function dispatch(param1:String, param2) : void
        {
            this.dispatchEvent(new PlayerEvent(param1, false, true, param2));
            return;
        }// end function

        public function togglePause() : void
        {
            if (_videoStatus.status == VideoStatus.PLAYING)
            {
                pause();
            }
            else if (_videoStatus.status == VideoStatus.PAUSED)
            {
                resume();
            }
            return;
        }// end function

        override public function set width(param1:Number) : void
        {
            background.width = param1;
            video.width = param1;
            return;
        }// end function

        public function onXMPData(param1:Object) : void
        {
            trace("onXMPData:", param1);
            return;
        }// end function

        protected function resetBody() : void
        {
            if (netConnetion)
            {
                netConnetion.close();
                netConnetion.removeEventListener(NetStatusEvent.NET_STATUS, connectStatus);
            }
            netConnetion = new NetConnection();
            netConnetion.addEventListener(NetStatusEvent.NET_STATUS, connectStatus);
            netConnetion.connect(null);
            createNetStream();
            video.attachNetStream(netStream);
            return;
        }// end function

        protected function streamStatus(event:NetStatusEvent) : void
        {
            var _loc_2:* = event.info.code;
            var _loc_3:* = event.info.level;
            switch(_loc_2)
            {
                case "NetStream.Play.Start":
                {
                    break;
                }
                case "NetStream.Buffer.Empty":
                {
                    bufferEmptyHlr();
                    break;
                }
                case "NetStream.Buffer.Full":
                {
                    bufferFullHlr();
                    break;
                }
                case "NetStream.Buffer.Flush":
                {
                    break;
                }
                case "NetStream.Play.Stop":
                {
                    completeHlr();
                    break;
                }
                case "NetStream.Pause.Notify":
                {
                    break;
                }
                case "NetStream.Unpause.Notify":
                {
                    break;
                }
                case "NetStream.Seek.Notify":
                {
                    break;
                }
                case "NetStream.Play.Failed":
                case "NetStream.Play.StreamNotFound":
                case "NetStream.Play.FileStructureInvalid":
                case "NetStream.Play.NoSupportedTrackFound":
                {
                    errorHlr(_loc_2);
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
            if (_loc_3 == INFO_STATUS)
            {
            }
            else if (_loc_3 == INFO_ERROR)
            {
            }
            streamStatusChange();
            return;
        }// end function

        public function get loadSize() : Number
        {
            if (netStream)
            {
                return netStream.bytesLoaded;
            }
            return 0;
        }// end function

        protected function bufferFullHlr() : void
        {
            dispatch(PlayerEvent.STREAM_BUFFER_FULL, null);
            firstBufferEmpty = true;
            if (_videoStatus.status == VideoStatus.LOAD_TO_PLAY)
            {
                changeStatus(VideoStatus.READY);
                changeStatus(VideoStatus.PLAYING);
            }
            else if (_videoStatus.status < VideoStatus.LOAD_TO_PLAY && !bugBufferFull)
            {
                bugBufferFull = true;
            }
            else if (_videoStatus.status == VideoStatus.BUFFERING)
            {
                changeStatus(VideoStatus.PLAYING);
            }
            return;
        }// end function

        public function get mute() : Boolean
        {
            return _mute;
        }// end function

        public function get volume() : Number
        {
            return _volume;
        }// end function

        public function get playTime() : Number
        {
            if (netStream)
            {
                return netStream.time;
            }
            return 0;
        }// end function

        public function onCuePoint(param1:Object) : void
        {
            trace("onCuePoint:", param1);
            return;
        }// end function

        public function resume() : void
        {
            if (_videoStatus.status == VideoStatus.PAUSED)
            {
                netStream.resume();
                changeStatus(VideoStatus.PLAYING);
            }
            return;
        }// end function

        public function get totalTime() : Number
        {
            return _metaData ? (_metaData.totalTime) : (0);
        }// end function

        public function set mute(param1:Boolean) : void
        {
            _mute = param1;
            if (_mute)
            {
                setStreamVolume(0);
            }
            else
            {
                setStreamVolume(_volume);
            }
            return;
        }// end function

        public function play() : void
        {
            if (_videoStatus.status == VideoStatus.LOADING)
            {
                netStream.resume();
                changeStatus(VideoStatus.LOAD_TO_PLAY);
                if (bugBufferFull)
                {
                    bufferFullHlr();
                }
            }
            return;
        }// end function

        public function set volume(param1:Number) : void
        {
            param1 = MathUtil.unit(param1);
            _volume = param1;
            setStreamVolume(_volume);
            return;
        }// end function

        protected function setStreamVolume(param1:Number) : void
        {
            var _loc_2:SoundTransform = null;
            if (netStream)
            {
                _loc_2 = netStream.soundTransform;
                _loc_2.volume = param1;
                netStream.soundTransform = _loc_2;
            }
            return;
        }// end function

        protected function bufferEmptyHlr() : void
        {
            dispatch(PlayerEvent.STREAM_BUFFER_EMPTY, null);
            if (firstBufferEmpty == false)
            {
                firstBufferEmpty = true;
                return;
            }
            if (_videoStatus.status == VideoStatus.PLAYING && netStream.bytesLoaded < netStream.bytesTotal)
            {
                changeStatus(VideoStatus.BUFFERING);
            }
            return;
        }// end function

        protected function changeStatus(param1:int) : void
        {
            _videoStatus.status = param1;
            dispatch(PlayerEvent.CHANGE_VIDEO_STATUS, _videoStatus.status);
            trace("VideoChangeStatus：", _videoStatus);
            return;
        }// end function

        protected function _load(param1:String) : void
        {
            _playURL = param1;
            resetBody();
            netStream.play(param1);
            netStream.pause();
            dispatch(PlayerEvent.CHANGE_STREAM, _playURL);
            return;
        }// end function

        protected function errorHlr(param1:String) : void
        {
            trace("Video Error Code：", param1);
            changeStatus(VideoStatus.ERROR);
            return;
        }// end function

        public function setWidthHeight(param1:Number, param2:Number) : void
        {
            background.width = param1;
            background.height = param2;
            mask.width = param1;
            mask.height = param2;
            video.width = param1;
            video.height = param2;
            return;
        }// end function

        public function get videoStatus() : VideoStatus
        {
            return _videoStatus;
        }// end function

        override public function set height(param1:Number) : void
        {
            background.height = param1;
            video.height = param1;
            return;
        }// end function

        public function reset() : void
        {
            _metaData = null;
            _playURL = "";
            resetBody();
            changeStatus(VideoStatus.INITED);
            return;
        }// end function

        public function onMetaData(param1:Object) : void
        {
            if (_metaData != null)
            {
                return;
            }
            if (!param1.filesize)
            {
                param1.filesize = netStream.bytesTotal;
            }
            _metaData = new MetaData(param1);
            trace("onMetaData ", _metaData.keyframesLength);
            dispatch(PlayerEvent.RECEIVE_METADATA, _metaData);
            return;
        }// end function

        public function get metaData() : MetaData
        {
            return _metaData;
        }// end function

        protected function createNetStream() : void
        {
            if (netStream)
            {
                netStream.close();
                netStream.removeEventListener(NetStatusEvent.NET_STATUS, streamStatus);
            }
            netStream = new NetStream(netConnetion);
            firstBufferEmpty = false;
            netStream.bufferTime = _bufferTime;
            volume = _volume;
            mute = _mute;
            netStream.client = this;
            netStream.addEventListener(NetStatusEvent.NET_STATUS, streamStatus);
            return;
        }// end function

        protected function streamStatusChange() : void
        {
            return;
        }// end function

        public function get totalSize() : Number
        {
            if (_metaData)
            {
                return _metaData.totalSize;
            }
            if (netStream)
            {
                return netStream.bytesTotal;
            }
            return 0;
        }// end function

        public function load(param1:String) : void
        {
            if (param1)
            {
                changeStatus(VideoStatus.LOADING);
                _metaData = null;
                _load(param1);
                dispatchEvent(new PlayerEvent(PlayerEvent.CHANGE_URL, false, true, _playURL));
            }
            return;
        }// end function

        public function replay() : void
        {
            if (_videoStatus.status == VideoStatus.COMPLETED)
            {
                seek(0);
            }
            return;
        }// end function

        public function onLastSecond(param1:Object) : void
        {
            trace("onLastSecond:", param1);
            return;
        }// end function

        protected function playerTimerHlr(event:TimerEvent) : void
        {
            var _loc_2:Object = {totalTime:totalTime, playTime:playTime, loadSize:loadSize, totalSize:totalSize};
            dispatch(PlayerEvent.UPDATE, _loc_2);
            return;
        }// end function

        protected function completeHlr() : void
        {
            changeStatus(VideoStatus.COMPLETED);
            return;
        }// end function

        public function pause() : void
        {
            if (_videoStatus.status == VideoStatus.PLAYING || _videoStatus.status == VideoStatus.BUFFERING)
            {
                netStream.pause();
                changeStatus(VideoStatus.PAUSED);
            }
            return;
        }// end function

        public function seek(param1:Number) : void
        {
            if (_videoStatus.status == VideoStatus.COMPLETED)
            {
                netStream.resume();
                changeStatus(VideoStatus.PLAYING);
            }
            if (_videoStatus.status == VideoStatus.PLAYING || _videoStatus.status == VideoStatus.PAUSED || _videoStatus.status == VideoStatus.BUFFERING)
            {
                netStream.seek(param1);
            }
            return;
        }// end function

        protected function connectStatus(event:NetStatusEvent) : void
        {
            var _loc_2:* = event.info.code;
            var _loc_3:* = event.info.level;
            if (_loc_3 == INFO_STATUS)
            {
            }
            else if (_loc_3 == INFO_ERROR)
            {
            }
            return;
        }// end function

        public function close() : void
        {
            changeStatus(VideoStatus.STOPPED);
            _metaData = null;
            _playURL = "";
            resetBody();
            return;
        }// end function

    }
}
